package com.itlong.cloud.controller.user;

import com.itlong.cloud.POJO.DTO.property.PropertyEquipRecordGetDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipRecordGetVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.equip.property.IPropertyEquipRecordServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      物业通行记录Controller
 * </desc>
 *
 * @createDate 2017/10/11
 */
@RestController
@RequestMapping("/user/equipRecord")
public class PropertyEquipRecordController {

    @Autowired
    IPropertyEquipRecordServiceClient iPropertyEquipRecordServiceClient;


    /**
     * <desc>
     *     分页查询通行记录
     * <desc/>
     *@param  propertyEquipRecordGetDTO  分页查询通行记录
     * @return  MessageVO 标准格式化响应结果
     * @author Biao.R
     * @createDate
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/getByPage")
    public Object getEquipRecordByPage(PropertyEquipRecordGetDTO propertyEquipRecordGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyEquipRecordGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyEquipRecordGetDTO.class, request);
            if (propertyEquipRecordGetDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            PropertyEquipTypeEnum equipTypeEnum = PropertyEquipTypeEnum.getByEquipType(propertyEquipRecordGetDTO.getDeviceType());
            //判断通行记录类型
            if(equipTypeEnum.getType()==-1){
                return new MessageVO(PropertyErrorCodeEnum.EQUIP_DEVICETYPE_IS_ILLEGAL.getErrorCode());
            }

            Page<PropertyEquipRecordGetVO> result = iPropertyEquipRecordServiceClient.getEquipRecordByPage(propertyEquipRecordGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), result);
        } catch (Exception e) {
            throw new DataAccessException("【物业_用户_通行记录】分页查询失败", e);
        }
    }

    /**
     * <desc>
     *     将通行记录中开门方式为动态密码和二维码的旧数据迁移到访客通行记录表中
     * <desc/>
     *@param  propertyEquipRecordGetDTO  分页查询通行记录
     * @return  MessageVO 标准格式化响应结果
     * @author Juguang.S
     * @createDate 2018/08/13
     */
    @ResponseBody
    @PostMapping(path = "/getVistitorInfo")
    public Object getVistitorInfo(PropertyEquipRecordGetDTO propertyEquipRecordGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyEquipRecordGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyEquipRecordGetDTO.class, request);

            iPropertyEquipRecordServiceClient.getVistitorInfo(propertyEquipRecordGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_用户_通行记录】迁移到访客通行记录表中失败", e);
        }
    }

    /**
     * <desc>
     *     查询项目的收费模式
     * <desc/>
     *
     * @param  projectId
     * @return  MessageVO 标准格式化响应结果
     * @author Jiaqi.X
     * @createDate 2019/10/23
     */
    @ResponseBody
    @PostMapping(path = "/searchProjectChargeModel")
    public Object searchProjectChargeModel(String projectId) throws Exception {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            projectId = request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyEquipRecordServiceClient.searchProjectChargeModel(projectId));
        }catch(Exception e){
            throw new DataAccessException("【物业_用户_通行记录】查询项目的收费模式失败",e);
        }
        return messageVO;
    }

}